from django.http import HttpResponse, Http404
from django.template import Context, loader, RequestContext
from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_protect
from django.contrib import messages

from ocms.accounts.forms import UserForm

@login_required
@csrf_protect    
def profile(request):
    if request.method == 'POST':
        form =  UserForm(request.POST, instance=request.user)
        if form.is_valid():
            form.save()
            messages.success(request, 'Profile updated.')
            return redirect('/accounts/profile/')
        else:
            messages.error(request, 'Please correct the errors below')
            form = UserForm(instance=request.user)
            c = {
                 'form': form, 
                 }
            return render_to_response('registration/profile.html', c , context_instance=RequestContext(request))
    else:
        form = UserForm(instance=request.user)
        c = {
             'form': form, 
             }
        return render_to_response('registration/profile.html', c , context_instance=RequestContext(request))